﻿<table style="width:800px;margin:auto" border="1">
  <caption>
    DANH SÁCH APPS<br />
    <br />
<?php
    if(!empty($_GET['idCategory'])){
$idCategory=round($_GET['idCategory']);}
else $idCategory=0;
?>
    <div id="search_space">
        <input class="search" type="text" name="search" id="search" onkeypress="if(event.keyCode==13) window.location='?mod=app&idCategory=<?php echo $idCategory?>&kw='+this.value"></input>
        <a onclick="window.location='?mod=app&idCategory=<?php echo $idCategory?>&kw='+document.getElementById('search').value" href="#" class="searchbtn"></a>
    </div>
	<select onchange="window.location='?mod=app&idCategory='+this.value">
	<option value="-1">--- Choose Category ---</option>
	<?php
		
	if(!empty($_GET['idCategory'])){
$idCategory=round($_GET['idCategory']);
$sql='select * from `category` where id='.$idCategory;

}else{
	
		$sql='select * from `category`';}
		$rs=mysqli_query($conn,$sql);
		while($r=mysqli_fetch_assoc($rs))
		{
	?>
    		<option <?php if($r['id']==$idCategory) echo 'selected' ?> value="<?php echo $r['id']?>"><?php echo $r['name']?></option>
    <?php	
        }
	?>
    
    </select>
  </caption>
  <tr>
    <th width="58" scope="col">STT</th>
    <th width="74" scope="col">ID</th>
    <th width="231" scope="col">Name</th>
    <th width="119" scope="col">Image</th>
    <th width="138" scope="col">Show/Hide</th>
    <th width="140" scope="col">Tools</th>
  </tr>
<?php
        if(!empty($_GET['kw'])){
            $kw=$_GET['kw'];
            if($idCategory == 0){
                $sql="SELECT * FROM app t1 INNER JOIN category_app t2 ON t1.id = t2.app_id where t1.name like '%$kw%'";
            }else{
                $sql="SELECT * FROM app t1 INNER JOIN category_app t2 ON t1.id = t2.app_id where t2.category_id='".$idCategory."' and t1.name like '%$kw%'";
            }
        }else{
            if($idCategory == 0){
                $sql="SELECT * FROM app t1 INNER JOIN category_app t2 ON t1.id = t2.app_id";
            }else{
                $sql="SELECT * FROM app t1 INNER JOIN category_app t2 ON t1.id = t2.app_id where t2.category_id='".$idCategory."'";
            }
        }
        
	
	$rs=mysqli_query($conn, $sql);
	$stt=1;
	$AnHien=array(0=>'<span style="color:red">Ẩn</span>',1=>'Hiện');
	while($r=mysqli_fetch_assoc($rs))
	{
?>
  <tr>
    <td align="center"><?php echo $stt++?></td>
    <td align="center">&nbsp;<?php echo $r['id']?></td>
    <td align="left">&nbsp;<?php echo $r['name']?></td>
    <td align="center"><a href="index.php?mod=detail&idapp=<?php echo $r['id']?>"><img class="icon-app" src="img/app/<?php echo $r['image']?>"/></a></td>
    <td align="center">&nbsp;<?php echo $AnHien[$r['show_hide']]?></td>
    <td align="center"><a href="?mod=app_edit&id=<?php echo $r['id'] ?>"><img src="admin/img/b_edit.png" width="16" height="16" alt="Sửa"></a>&nbsp;<a onclick="return confirm('Bạn có chắc chắn muốn xóa hay không ?')" href="?mod=app_delete&id=<?php echo $r['id'] ?>"><img src="admin/img/b_drop.png" width="16" height="16" alt="Xóa"></a></td>
  </tr>
<?php
	}
?>
  <tr>
    <td colspan="6" align="center"><label>
      <input onClick="window.location='?mod=app_add&idCategory=<?php echo $idCategory ?>'" type="button" name="app_add" id="app_add" value="Add App">
    </label></td>
  </tr>
</table>
